# - Locate OpenSSL library
# This module defines
# OPENSSL_INCLUDE_DIRS   - where to find openssl/ssl.h, etc.
# OPENSSL_LIBRARIES      - List of libraries when using openssl.
# OPENSSL_FOUND          - True if openssl found.
# OPENSSL_VERSION_STRING - the version of openssl found (since CMake 2.8.8)

IF(OPENSSL_LIBRARIES AND OPENSSL_INCLUDE_DIR)
  # in cache already
  SET(OPENSSL_FIND_QUIETLY TRUE)
ENDIF(OPENSSL_LIBRARIES AND OPENSSL_INCLUDE_DIR)

FIND_PATH(OPENSSL_INCLUDE_DIR ssl.h
  PATH_SUFFIXES openssl
  $ENV{OPENSSL_DIR}/include
  /usr/local/include
  /usr/local/include/openssl
)

SET(LIBRARY_NAME_RELEASE libssl libssl.a)
SET(LIBRARY_NAME_DEBUG libssl libssl.a)

FIND_LIBRARY(OPENSSL_LIBRARIE_RELEASE
  NAMES ${LIBRARY_NAME_RELEASE}
  PATHS
  $ENV{OPENSSL_DIR}/lib
  /usr/local/lib64
)

FIND_LIBRARY(OPENSSL_LIBRARY_DEBUG
  NAMES ${LIBRARY_NAME_DEBUG}
  PATHS
  $ENV{OPENSSL_DIR}/lib
  /usr/local/lib64
)

SET(LIBRARY_NAME_CRYPTO libcrypto libcrypto.a)
FIND_LIBRARY(CRYPTO_LIBRARIE_RELEASE
  NAMES ${LIBRARY_NAME_CRYPTO}
  PATHS
  $ENV{OPENSSL_DIR}/lib
  /usr/local/lib64
)

IF(OPENSSL_INCLUDE_DIR)
  IF(OPENSSL_LIBRARIE_RELEASE AND CRYPTO_LIBRARIE_RELEASE)
    # Case where both Release and Debug versions are provided
    SET(OPENSSL_FOUND TRUE)
    #SET(OPENSSL_LIBRARIES optimized ${OPENSSL_LIBRARIE_RELEASE} debug ${OPENSSL_LIBRARY_DEBUG})
    SET(OPENSSL_LIBRARIES "${OPENSSL_LIBRARIE_RELEASE};${CRYPTO_LIBRARIE_RELEASE}" CACHE STRING "OpenSSL Libraries")
  ENDIF(OPENSSL_LIBRARIE_RELEASE AND CRYPTO_LIBRARIE_RELEASE)
ENDIF(OPENSSL_INCLUDE_DIR)

IF(OPENSSL_FOUND)
  IF(NOT OPENSSL_FIND_QUIETLY)
    MESSAGE(STATUS "Found OpenSSL: ${OPENSSL_INCLUDE_DIR} ${OPENSSL_LIBRARIES}")
  ENDIF(NOT OPENSSL_FIND_QUIETLY)
ELSE(OPENSSL_FOUND)
  IF(NOT OPENSSL_FIND_QUIETLY)
    MESSAGE(STATUS "Warning: Unable to find OpenSSL! INCLUDE: ${OPENSSL_INCLUDE_DIR}  LIB:${OPENSSL_LIBRARIES}  ")
  ENDIF(NOT OPENSSL_FIND_QUIETLY)
ENDIF(OPENSSL_FOUND)

MARK_AS_ADVANCED(OPENSSL_LIBRARIE_RELEASE OPENSSL_LIBRARY_DEBUG)





